package com.gwtjs.icustom.controller;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwtjs.icustom.entity.PageVO;
import com.gwtjs.icustom.entity.PagedResult;
import com.gwtjs.icustom.entity.ResultWrapper;
import com.gwtjs.icustom.mapper.UserMyMapper;
import com.gwtjs.icustom.pojo.User;

@RestController @RequestMapping("/user/my")
public class UserMyController {

    @Autowired
    private UserMyMapper userMapper;
    private final static Logger logger = LoggerFactory.getLogger(UserMyController.class);

    @GetMapping("/queryUserList/{mysqlStartIndex}/{mysqlEndIndex}")
    public PagedResult<User> queryUserListPage(User record, PageVO page){
    	PagedResult<User> pagedResult = userMapper.findUserListPage(record,page);
    	logger.info("Paged Result:",pagedResult.toString());
        return pagedResult;
    }

    @GetMapping("/{id}")
    public ResultWrapper queryUserById(@PathVariable("id") Integer id){
    	User user = userMapper.queryUserById(id);
        return ResultWrapper.successResult(user);

    }

    /**
     * @apiNote ":8080/user/my/add" 
     * {
		    "account": "flash8627",
		    "password": "flash8627",
		    "username": "flash8627",
		    "address": "flash8627"
		}
     * @param user
     * @return
     */
    @PostMapping("/add")
    public ResultWrapper addUser(@RequestBody User user){
        logger.info("\nadd user:",user.toString());
        userMapper.addUser(user);
        return ResultWrapper.successResult(user);
    }
    
    /**
     * @apiNote ":8080/user/update" 
     * {
		    "id":3,
			"account": "old_8627",
		    "password": "flash8627old",
		    "username": "flash8627old",
		    "address": "flash8627old"
		}
     * @param user
     * @return
     */
    @PutMapping("/update")
    public ResultWrapper updateUser(@RequestBody User user){
        logger.info("\nupdate user:",user.toString());
        userMapper.updateUser(user);
        return ResultWrapper.successResult(user);
    }

    /**
     * @apiNote ":8080/user/delete" 
     * {"ids":[1,3]}
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResultWrapper batchDeleteUser(@RequestBody List<User> records){
    	logger.info("\ndelete user:",records.toString());
        userMapper.deleteUser(records);
        return ResultWrapper.successResult(records);
    }

}
